package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.service.ProjectRTypeDetailService;
import com.gisuni.sddc.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
* Title: 项目配置项目类型
* Description: 项目关联项目类型,生成项目对应的明细记录
* @author landa
* @date 2017年3月31日 上午11:07:23
 */
@IocBean(singleton = false)
@At("/client/backgroundManagement/ProjectRTypeDetailController")
public class ProjectRTypeDetailController{

	private Log log = Logs.getLog(ProjectRTypeDetailController.class);
 
	@Inject
    private ProjectRTypeDetailService projectRTypeDetailService;

 
	/**
	 * 
	 * @param projectName 项目名称
	 * @param projectType 项目类型(如:广州留用地模式)) ; 未加(中长期/短期/外业拓展等项目类型的项目类型
	 * @param page
	 * @param rows
	 * @param request
	 * @return
	 */
	@At("/dataGrid")
	@GET
	@POST
	@Ok("raw:json") 
    public Object dataGrid(@Param("projectName") String projectName,@Param("projectType") String projectType,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;	
			int to = (page)*rows;	
			res = projectRTypeDetailService.queryBaseEntityAll(from,to,projectName,projectType);
			r = Response.toJSONObject(res);
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg(e.getMessage());
			return res;
		}
		return r;
    }

 
    
	/**
	 * 编辑配置信息
	 * @param request
	 * @return
	 */
	@At("/edit")
	@GET
	@POST
	@Ok("json")
    public Object edit(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = projectRTypeDetailService.updateEntity(request); 
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
	
    
	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/projectRTypeEdit.jsp")
    public String editPage( HttpServletRequest request, String id,String type) {
		JSONObject obj  = projectRTypeDetailService.findEntityByIdAndType(id,type);
        request.setAttribute("entity",obj );
        request.setAttribute("projectType",request.getParameter("type") );
        return null;
    }

 
	/**
	 * 多条件查询匹配的项目
	 * @param request
	 * @return
	 */
	@At("/find")
	@GET
	@POST
	@Ok("json")
    public Object find(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = projectRTypeDetailService.find(request); 
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
	
	
	/**
	 * 导出中长期项目Excel
	 * @param departmentType
	 * @param projectType
	 * @param startdate
	 * @param enddate
	 * @param response
	 */
	@At("/exportZCQXMExcel")
	@GET
	@POST
	public void exportExcel(HttpServletRequest request,HttpServletResponse response) {
		try {
			String projectIds = request.getParameter("projectIds");
			projectRTypeDetailService.exportZCQXMExcel(projectIds,response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
 
	@At("/update")
	@GET
	@POST
	@Ok("json")
    public Object update(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		boolean r=false;
		try {
              r = projectRTypeDetailService.update(request); 
              if(r==true){
            	  res.setSuccess(true);  
              }
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
	
	
	@At("/getAllFitConditionProjectDetial/")
	@GET
	@POST
	@Ok("json")
	public  String getAllFitConditionProjectDetial(@Param(value="pkuuid") String pkuuid){
		String res = new String();
		try {
			res =  projectRTypeDetailService.getTimeNodeByTypeId(pkuuid);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
	}
	/**
	 * 根据前端传入的外业拓展id和项目名称在project_r_timenode 表中增加一条记录
	 * @param pId
	 * @param pname
	 * @return
	 */
	@At("/wytzRelevanceTypeGetNode/")
	@GET
	@POST
	@Ok("json")
	public  String wytzRelevanceTypeGetNode(@Param(value="Pid") String pId,@Param(value="Pname") String pName){
		String res = new String();
		try {
			res =  projectRTypeDetailService.WYTZRelevanceTypeGetNode(pId,pName);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
	}
	@At("/relevanceTypeGetNodeAndAttachment/")
	@GET
	@POST
	@Ok("json")
    public Map<String, String> relevanceTypeGetNodeAndAttachment(@Param(value="Pid") String pId,@Param(value="Pname") String pName){
		String nodeinfo = new String();
		String attachinfo=new String();
		Map< String, String> map=new HashMap<>();
		try {
			nodeinfo =  projectRTypeDetailService.GetNodeInfo(pId,pName);
			attachinfo=projectRTypeDetailService.GetNodeAttachments(pId,pName);
			map.put("nodeinfo", nodeinfo);
			map.put("attachinfo", attachinfo);
			System.out.println(nodeinfo);
			System.out.println(attachinfo);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return map;
    }
}
